package com.lmx.web.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.common.util.SMSUtil;
import com.lmx.persistence.service.UserService;
import com.lmx.repoDomain.tmodel.business.User;
import com.lmx.repoDomain.tmodel.unpersis.TempVerificationCode;

/**
 * 重置密码
 *
 * @author ytche
 * @date 2017年9月7日
 *
 */
@Controller
@ResponseBody
@RequestMapping(value = "/password", headers = "Accept=*/*", produces = "application/json")
public class PasswordController {
	@Autowired
	UserService<User> userService;

	@RequestMapping(value = "/sendResetCode", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> sendResetCode(@RequestParam String mobile) {
		Map<String, Object> rtn = Maps.newHashMap();
		List<User> users = this.userService.findUsersByLoginName(mobile);
		if (users == null || users.size() < 1) {
			rtn.put("msg", "不存在该手机号绑定的用户");
			rtn.put("success", false);
			rtn.put("status", 0);
			return rtn;
		}
		Integer code = SMSUtil.getInstance().sendResetCode(mobile);
		TempVerificationCode tempCodeEntity = new TempVerificationCode(mobile, code);
		ActionContextHolder.putTempCode(mobile, tempCodeEntity);
		rtn.put("msg", "已发送验证码");
		rtn.put("success", true);
		rtn.put("status", 1);
		return rtn;
	}

	@RequestMapping(value = "/checkCode", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public String checkCode(@RequestParam String mobile, @RequestParam Integer code) {
		Map<String, Object> rtn = Maps.newHashMap();
		if (StringUtils.isBlank(mobile) || code == null) {
			rtn.put("msg", "手机号或者验证码不能为空");
			rtn.put("success", false);
			rtn.put("status", 0);
			return CommonUtil.mapToJsonString(rtn);
		}
		if (!ActionContextHolder.getTempCode().containsKey(mobile)
				|| !(code.equals(ActionContextHolder.getTempCode().get(mobile).getCode()))) {
			rtn.put("msg", "手机号与验证码不匹配");
			rtn.put("success", false);
			rtn.put("status", 2);
		}
		rtn.put("msg", "验证通过");
		rtn.put("success", true);
		rtn.put("status", 1);
		return CommonUtil.mapToJsonString(rtn);
	}

	@RequestMapping(value = "/resetPassword", method = { RequestMethod.POST })
	@ResponseBody
	public String resetPassword(@RequestParam String mobile, @RequestParam String password) {
		Map<String, Object> rtn = Maps.newHashMap();
		if (StringUtils.isBlank(mobile) || StringUtils.isBlank(password)) {
			rtn.put("msg", "手机号或者密码不能为空");
			rtn.put("success", false);
			rtn.put("status", 0);
			return CommonUtil.mapToJsonString(rtn);
		}
		this.userService.resetPassword(mobile, password);
		rtn.put("msg", "重置密码成功");
		rtn.put("success", true);
		rtn.put("status", 1);
		return CommonUtil.mapToJsonString(rtn);
	}
}
